package com.ruoyi.web.controller.safety;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.xuehua.SnowflakeIdUtils;
import com.ruoyi.safety.domain.Adress;
import com.ruoyi.safety.domain.Device;
import com.ruoyi.safety.dto.AdressDto;
import com.ruoyi.safety.service.IAdressService;
import com.ruoyi.safety.service.IDeviceService;
import com.ruoyi.safety.vo.AdressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 地点管理Controller
 * 
 * @author lth
 * @date 2024-03-13
 */
@RestController
@RequestMapping("/adress/adress")
public class AdressController extends BaseController
{
    @Autowired
    private IAdressService adressService;

    @Autowired
    private IDeviceService deviceService;




    /**
     * 通过区域id获取地点信息
     */
    @GetMapping("/adressByAreaId/{areaId}")
    public List adressByAreaId(@PathVariable String areaId){
        List adressByAreaId = adressService.adressByAreaId(areaId);
        System.out.println("dahsdhaskdasidnsdkas" + adressByAreaId);
        return adressByAreaId;
        //return getDataTable(listByMineId);
    }

    /**
     * 查询地点管理列表
     */
    @PreAuthorize("@ss.hasPermi('adress:adress:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdressVo adressVo)
    {
        startPage();
        List<AdressDto> list = adressService.selectAdressDtoListByVo(adressVo);
        return getDataTable(list);
    }

//    /**
//     * 查询地点管理列表
//     */
//    @PreAuthorize("@ss.hasPermi('adress:adress:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(Adress adress)
//    {
//        startPage();
//        List<Adress> list = adressService.selectAdressList(adress);
//        return getDataTable(list);
//    }

    /**
     * 导出地点管理列表
     */
    @PreAuthorize("@ss.hasPermi('adress:adress:export')")
    @Log(title = "地点管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Adress adress)
    {
        List<Adress> list = adressService.selectAdressList(adress);
        ExcelUtil<Adress> util = new ExcelUtil<Adress>(Adress.class);
        util.exportExcel(response, list, "地点管理数据");
    }

    /**
     * 获取地点管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('adress:adress:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(adressService.selectAdressById(id));
    }

    /**
     * 新增地点管理
     */
    @PreAuthorize("@ss.hasPermi('adress:adress:add')")
    @Log(title = "地点管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Adress adress)
    {
        //判断某区域下是否有相同地址名
//        List<Adress> adressList = adressService.selectByAreaId(adress.getAreaId());
//        for (Adress adress1 : adressList) {
//            if (adress.getInstallAdress().equals(adress1.getInstallAdress())){
//                return error("该区域下已有该安装地址");
//            }
//        }
        //判断某区域下是否有相同地址名
        Adress adress1 = adressService.selectAdressByInstall(adress.getInstallAdress());
        if (adress1 != null){
            return error("该安装地址已存在");
        }
        adress.setId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        return toAjax(adressService.insertAdress(adress));
    }

    /**
     * 修改地点管理
     */
    @PreAuthorize("@ss.hasPermi('adress:adress:edit')")
    @Log(title = "地点管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Adress adress)
    {
        return toAjax(adressService.updateAdress(adress));
    }

    /**
     * 删除地点管理
     */
    @PreAuthorize("@ss.hasPermi('adress:adress:remove')")
    @Log(title = "地点管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        for (String id : ids) {
            //获取该id的地点信息
            Adress adress = adressService.selectAdressById(id);

            //查询设备表中的数据来判断该地点是否被使用
            Device device = new Device();
            device.setAreaId(adress.getAreaId());
            List<Device> devices = deviceService.selectDeviceList(device);//获取该区域下的所有安装地址名称进行对比
            for (Device device1 : devices) {
                if (adress.getInstallAdress().equals(device1.getInstallAdress())){
                    return error("["+adress.getInstallAdress()+"]安装地址下有关联设备，不能删除！");
                }
            }
        }

        return toAjax(adressService.deleteAdressByIds(ids));
    }

    /**
     * 查询风险区域
     */
    @PreAuthorize("@ss.hasPermi('adress:adress:list')")
    @GetMapping("/area")
    public List<Adress> area()
    {
        List<Adress> adressList = adressService.getArea();
        return adressList;
    }
}
